export class MathUtils {
    // byte 转MB String
    public static computeByteSize(limit: number): string {
        let size = "";
        if (limit < 0.1 * 1024) {                            //小于0.1KB，则转化成B
            size = limit.toFixed(2) + "B"
        } else if (limit < 0.1 * 1024 * 1024) {            //小于0.1MB，则转化成KB
            size = (limit / 1024).toFixed(2) + "KB"
        } else if (limit < 0.1 * 1024 * 1024 * 1024) {        //小于0.1GB，则转化成MB
            size = (limit / (1024 * 1024)).toFixed(2) + "MB"
        } else {                                            //其他转化成GB
            size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB"
        }

        let sizeStr = size + "";                        //转成字符串
        let index = sizeStr.indexOf(".");                    //获取小数点处的索引
        let dou = sizeStr.substr(index + 1, 2)            //获取小数点后两位的值
        if (dou == "00") {                                //判断后两位是否为00，如果是则删除00                
            return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
        }
        return size;
    }

    // 计算执行方法花费的时间
    public static computeCostTime(func: () => void): number {
        const startTime: number = new Date().getTime();
        func();
        const endTime: number = new Date().getTime();

        return endTime - startTime;
    }

    // map转数组 并按照规则排序
    public static mapToArray<T>(map: {}, condtion?: (a: T, b: T) => number): T[] {
        let arry: T[] = [];
        for (const key in map) {
            arry.push(map[key]);
        }
        if (condtion) {
            arry = arry.sort(condtion);
        }

        return arry;
    }

    // 输入一个数组 返回一个不包含重复数字的新数组
    public static unique<T>(arr: T[]): T[] {
        const newArr: T[] = [];
        for (const a of arr) {
            if (newArr.indexOf(a) === -1) {
                newArr.push(a);
            }
        }
        return newArr;
    }

    // 在数组中随机取某一个元素
    public static sample<T>(arr: T[]): T {
        return arr[Math.floor(Math.random() * arr.length)];
    }

    // 判断map 是否没有元素
    public static isMapNull(map: {}): boolean {
        for (const key in map) {
            return false;
        }
        return true;
    }

    // 将一个object 赋值给另一个object 不改变引用
    public static fillObjectData(objA: any, objB: any) {
        Object.keys(objA).forEach((key) => {
            const valueType: string = typeof objB[key];
            if (!objB[key]) {
                objA[key] = null;
            }

            objA[key] = objB[key];
        });

        Object.keys(objB).forEach((key) => {
            if (!objA[key]) {
                objA[key] = objB[key];
            }

        });
    }

    // 将字节转换为string
    public static byteToString(arr: Uint16Array): string {
        if (typeof arr === "string") {
            return arr;
        }
        let str = "";
        // tslint:disable-next-line: variable-name
        const _arr = arr;
        for (let i = 0; i < _arr.length; i++) {
            const one = _arr[i].toString(2);
            const v = one.match(/^1+?(?=0)/);
            if (v && one.length === 8) {
                const bytesLength = v[0].length;
                let store = _arr[i].toString(2).slice(7 - bytesLength);
                for (let st = 1; st < bytesLength; st++) {
                    store += _arr[st + i].toString(2).slice(2);
                }
                str += String.fromCharCode(parseInt(store, 2));
                i += bytesLength - 1;
            } else {
                str += String.fromCharCode(_arr[i]);
            }
        }
        return str;
    }

    // 获取map的元素总数
    public static getMapSum(map: {}): number {
        let sum = 0;
        for (const key in map) {
            sum += 1;
        }
        return sum;
    }

    // 根据条件查找数组里面的元素
    public static findCellInArray<T>(arr: T[], f: (cond: T) => boolean): T | undefined {
        for (const key in arr) {
            if (arr.hasOwnProperty(key)) {
                const element = arr[key];
                if (element) {
                    const b = f(element);
                    if (b) {
                        return element;
                    }
                }
            }
        }
        return undefined;
    }
    // 根据条件删除数组里的某个元素
    public static deleteCellInArray(arr: any[], callBack: (element: any) => boolean): any {

        for (let i = 0; i < arr.length; i++) {
            const element = arr[i];
            if (callBack(element)) {
                arr.splice(i, 1);
                break;
            }
        }
        return arr;

    }
    // 根据条件查找map里面的元素
    public static findCellInMap<T>(map: {}, callBack: (element: T) => boolean): T {
        for (const key in map) {
            const element = map[key];
            if (callBack(element)) {
                return element;
            }
        }
        return undefined;
    }

    // 交换两个数据
    public static swap(a: any, b: any): void {
        const temp: any = a;
        a = b;
        b = a;
    }

    // 获取某个范围的随机数cl
    public static getRandomInt(min: number, max: number): number {
        const Range: number = max - min;
        const rand: number = Math.random();
        return (min + Math.round(rand * Range));
    }


}